﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ManageBanking.Models;
namespace ManageBanking.Controllers
{
    public class CashierController : Controller
    {
        //
        // GET: /Cashier/
        T_AccountModels tam = new T_AccountModels();
        ManageUserModels mu = new ManageUserModels();
        ManageBanking.Areas.Admin.Models.ManageUserModels admu = new ManageBanking.Areas.Admin.Models.ManageUserModels();
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddAccUser()
        {
            if (Session["Login"] != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    CashierModels mam = new CashierModels();
                    ViewData["menuleft_bottom"] = "AddUsername";
                    ViewData["Result"] = Session["Result"];
                    Session.Remove("Result");
                    ViewData["listtypeacc"] = mam.ListTypeAcc();
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        }
        public ActionResult Withdaw_Dep(string id)
        {
            if (Session["Login"] != null && id != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    ViewData["menuleft_bottom"] = "DepositeWithaw";
                    int id1 = 0;
                    int id2 = Convert.ToInt32(id);
                    id1 = tam.TotalUserNoActive(id2);
                    ViewData["totaluser"] = id1;
                    ViewData["info"] = tam.infodep(id2);
                    ViewData["money"] = tam.TotalMoneyNoActive(id2);
                    ViewData["Check"] = Session["Check"];
                    Session.Remove("Check");
                    ViewData["id"] = id;
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
       
            
        }
        public ActionResult ShowDep_With(string username)
        {
            if (Session["Login"] != null && username !=null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    ManageUserModels mu = new ManageUserModels();
                    LoginModels lg = new LoginModels();
                    int id = 0;
                    if (mu.CheckUSer2(username.Trim()) == true)
                    {
                        Session["Check"] = "true";
                        Username ac = lg.Login_Acc(username.Trim());
                        id = ac.IdUsername;
                    }
                    else
                    {
                        Session["Check"] = "No Data Found";


                    }
                    return RedirectToAction("Withdaw_Dep/" + id);
                }
                else
                    return RedirectToAction("Home", "Login");

            }
            else
                return RedirectToAction("Home", "Login");

           
            
        }
        public ActionResult ResolveAddAccount(decimal money, string IdUsername, string IdType)
        {

            if (Session["Login"] != null && IdUsername != null && IdType != null)
            {
              
                Username us1 = (Username)Session["Login"];
                if (us1.IdTypeUser == 3)
                {
                    string idaccount;
                    ManageUserModels mu = new ManageUserModels();
                    CashierModels mam = new CashierModels();
                    OnlineBankingDataContext odbc = new OnlineBankingDataContext();
                    Username us = odbc.Usernames.Where(a => a.Username1.Equals(IdUsername.Trim())).First();
                    if (mu.CheckUSer2(IdUsername) == false)
                        Session["Check"] = "Username Not Exist,Try Again";
                    else if (mam.AddAccount(us1.IdUsername, IdUsername.Trim(), Convert.ToInt32(IdType.Trim()), out idaccount) == true)
                    {
                        mam.ActivateAccountNoId(0, Convert.ToDecimal(money), idaccount,us1.IdUsername);
                        Session["Check"] = "true";
                    }

                    else
                        Session["Check"] = "Add Fail,Try Again";
                    return RedirectToAction("AddAcountUser");
                }
                else
                    return RedirectToAction("Home", "Login");
            
            }
            else
                return RedirectToAction("Home", "Login");
        }


        public ActionResult AddAcountUser()
        {

            if (Session["Login"] != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    ViewData["menuleft_bottom"] = "AddAccount";
                    CashierModels mam = new CashierModels();
                    ViewData["listtypeacc"] = mam.ListTypeAcc();
                    ViewData["Check"] = Session["Check"];
                    Session.Remove("Check");
                    return View();
                }
                return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        }
        public ActionResult ResolveWit_Dep(string id,string money,int type,string totalmoney,string idaccount,string idadmin)
        {
            if (Session["Login"] != null && id != null && money != null && type != 0 && totalmoney != null && idaccount != null && idadmin != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    CashierModels cs = new CashierModels();
                    if (Convert.ToDecimal(money) > 20000)
                    {
                        Session["Check"] = "Money Must Be Below 20.000USD";
                        return RedirectToAction("Withdaw_Dep/" + id);

                    }
                    if (Convert.ToDecimal(money) < 100)
                    {
                        Session["Check"] = "Money Must Be Above 100USD";
                        return RedirectToAction("Withdaw_Dep/" + id);

                    }
                    if (type == 1 && Convert.ToDecimal(totalmoney) == 0)
                    {
                        if (cs.ActivateAccount(Convert.ToDecimal(totalmoney), Convert.ToDecimal(money),idaccount, Convert.ToInt32(idadmin), Convert.ToInt32(id)) == true)
                        {
                            Session["Check"] = "true";
                        }
                        else
                        {
                            Session["Check"] = "Money To Activate This Account Must Be Above 100 USD";

                        }


                    }
                    else if (type == 1)
                    {
                        if (cs.DepositAcc(Convert.ToDecimal(totalmoney), Convert.ToDecimal(money), idaccount, Convert.ToInt32(idadmin)) == true)
                        {

                            Session["Check"] = "true";
                        }
                        else
                            Session["Check"] = "Your Account Is Not Actiavate,So You Need To Activate First";

                    }
                    else if (type == 2)
                    {
                        if (cs.WithDrawAcc(Convert.ToDecimal(totalmoney), Convert.ToDecimal(money), idaccount, Convert.ToInt32(idadmin)) == true)
                        {

                            Session["Check"] = "true";
                        }
                        else
                            Session["Check"] = "Your Acc Must Have At Least 100 USD";

                    }
                    else if (type == 3)
                    {
                        decimal money1 = 0;
                        if (cs.DeActivateAcc(Convert.ToDecimal(totalmoney), money1, idaccount, Convert.ToInt32(idadmin), Convert.ToInt32(id)) == true)
                        {
                            Session["Check"] = "true";


                        }
                        else
                            Session["Check"] = "Your Account Is Not Activate";

                    }


                    return RedirectToAction("Withdaw_Dep/" + id);
                }
                else
                    return RedirectToAction("Home", "Login");
        
            }
            else
                return RedirectToAction("Home","Login");
        }

        public ActionResult ResolveAddAcc(string money, string IdType,string TypeUser, string username, string password, string name, string identificard, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string phase,string idadmin)
        {
            if (Session["Login"] != null && TypeUser != null && username != null && password != null && name != null && name != null && identificard != null && sex != null && birth != null && address != null && Email != null && PlaceBirth != null && job != null && phase != null && idadmin != null)
            {
                Username us = (Username)Session["Login"];
                
                if (us.IdTypeUser == 3)
                {
                    
                    if (mu.CheckUSer(username) == true)
                    {
                        Session["Result"] = "Username is already used";
                        return RedirectToAction("AddAccUser");
                    }
                    else if (mu.AnswerIdentity(identificard) == true)
                    {
                        Session["Result"] = "Identification card is already exist";
                        return RedirectToAction("AddAccUser");

                    }
                    else if (mu.Answer_Email(Email) == true)
                    {
                        Session["Result"] = "Email is already exist";
                        return RedirectToAction("AddAccUser");

                    }


                    else
                    {
                        CashierModels mam = new CashierModels();
                        admu.AddAcc(Convert.ToInt32( TypeUser), username, password, name, identificard, sex, birth, address, Email, PlaceBirth, job, phase, Convert.ToInt32( idadmin));
                        string idaccount;
                        mam.AddAccount(us.IdUsername,username.Trim(), Convert.ToInt32(IdType.Trim()),out idaccount);
                       mam.ActivateAccountNoId(0, Convert.ToDecimal(money),idaccount, Convert.ToInt32(idadmin));

                        Session["Result"] = "true";
                        Session.Remove("captch");
                        return RedirectToAction("AddAccUser");
                    }
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");

        }
        public ActionResult CheckChangeInfo()
        {
            if (Session["Login"] != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    ViewData["menuleft_bottom"] = "Change-Information";
                    ViewData["CheckII"] = Session["CheckII"];
                    ViewData["user1"] = Session["user1"];
                    Session.Remove("CheckII");
                    Session.Remove("user1");
                    Session.Remove("User");
                    return View();
                }

                return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");

        }

        public ActionResult ResolveChangeInfo(string username, string idcard)
        {
            if (Session["Login"] != null && username != null && idcard != null)
            {
                Username us = (Username)Session["Login"];
                if (us.IdTypeUser == 3)
                {
                    RequestAdminModels ram = new RequestAdminModels();
                    LoginModels lg = new LoginModels();
                    if (mu.CheckUSer(username) == true)
                    {
                        Session["User"] = lg.Login_Acc(username);


                    }

                    Username ac = (Username)Session["User"];

                    if (mu.CheckUSer(username) == false)
                    {
                        Session["CheckII"] = "Username not exist,Try Again ";
                        Session["user1"] = username;

                        return RedirectToAction("CheckChangeInfo");

                    }
                    else if (mu.AnswerIdentity1(ac.Username1, idcard) == false)
                    {

                        Session["CheckII"] = "Wrong Identify Card,Try Again";



                        return RedirectToAction("CheckChangeInfo");

                    }
                    else
                    {
                        Session["CheckII"] = "true";
                        return RedirectToAction("ChangeInfor");

                    }
                }
                else
                    return RedirectToAction("Home", "Login");
           
            }
            else
                return RedirectToAction("Home", "Login");
           

            

         }
        public ActionResult ChangeInforCashier()
        {
            if (Session["Login"] != null)
            {
                Username us = (Username)Session["Login"];
                Session.Remove("User");
                if (us.IdTypeUser == 3)
                {
                    ViewData["menuleft_bottom"] = "ChangeInformationCash";
                    ViewData["User"] = Session["Login"];
                    ViewData["Result"] = Session["Result"];
                    Session.Remove("Result");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        
        }

        public ActionResult ChangeInfor()
        {
            if (Session["Login"] != null && Session["User"]!=null)
            {
                Username us = (Username)Session["Login"];
                Session.Remove("CheckII");
                if (us.IdTypeUser == 3)
                {
                    ViewData["menuleft_bottom"] = "Change-Information";
                    ViewData["User"] = Session["User"];
                    ViewData["Result"] = Session["Result"];
                    Session.Remove("Result");
                    return View();
                }
                else
                    return RedirectToAction("Home", "Login");
            }
            else
                return RedirectToAction("Home", "Login");
        }

        public ActionResult ResolveInformationCashier(string username, string name, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string identificard, string SecPass, int idadmin, int iduser)
        {
            if (Session["Login"] != null && username != null && name != null && sex != null && birth != null && address != null && address != null && Email != null && PlaceBirth != null && job != null && identificard != null && idadmin != 0 && iduser != 0)
            {
                Username us1 = (Username)Session["Login"];
                if (us1.IdTypeUser == 3)
                {
                    Username us = (Username)Session["Login"];
                    if (mu.CheckUSer(username.Trim()) == true && us.Username1 != username.Trim())
                    {
                        Session["Result"] = "Username is already used";
                        return RedirectToAction("ChangeInfor");
                    }
                    else if (mu.AnswerIdentity(identificard) == true && us.IdentityCard != identificard.Trim())
                    {
                        Session["Result"] = "Identification card is already exist";
                        return RedirectToAction("ChangeInfor");

                    }
                    else if (mu.Answer_Email(Email) == true && us.Email != Email.Trim())
                    {
                        Session["Result"] = "Email is already exist";
                        return RedirectToAction("ChangeInfor");

                    }


                    else
                    {
                        if (mu.ChangeInformation(username, name, sex, birth, address, Email, PlaceBirth, job, identificard, SecPass, idadmin, iduser) == true)
                        {
                            Session["Result"] = "true";
                            Session.Remove("User");
                            return RedirectToAction("ChangeInforCashier");
                        }
                        else
                        {
                            Session["Result"] = "Error,Try Again";

                        }

                    }
                    return RedirectToAction("ChangeInfor");
                }
                else
                    return RedirectToAction("Home", "Login");

            }
            else
                return RedirectToAction("Home", "Login");
        }


        public ActionResult ResolveInformation(string username, string name, string sex, DateTime birth, string address, string Email, string PlaceBirth, string job, string identificard, string SecPass, int idadmin, int iduser)
        {
            if (Session["Login"] != null && username != null && name != null && sex != null && birth != null && address != null && address != null && Email != null && PlaceBirth != null && job != null && identificard != null && idadmin != 0 && iduser != 0)
            {
                Username us1 = (Username)Session["Login"];
                if (us1.IdTypeUser == 3)
                {
                    Username us = (Username)Session["User"];
                    if (mu.CheckUSer(username.Trim()) == true && us.Username1 != username.Trim())
                    {
                        Session["Result"] = "Username is already used";
                        return RedirectToAction("ChangeInfor");
                    }
                    else if (mu.AnswerIdentity(identificard) == true && us.IdentityCard != identificard.Trim())
                    {
                        Session["Result"] = "Identification card is already exist";
                        return RedirectToAction("ChangeInfor");

                    }
                    else if (mu.Answer_Email(Email) == true && us.Email != Email.Trim())
                    {
                        Session["Result"] = "Email is already exist";
                        return RedirectToAction("ChangeInfor");

                    }


                    else
                    {
                        if (mu.ChangeInformation(username, name, sex, birth, address, Email, PlaceBirth, job, identificard, SecPass, idadmin, iduser) == true)
                        {
                            Session["CheckII"] = "true";
                            Session.Remove("User");
                            return RedirectToAction("CheckChangeInfo");
                        }
                        else
                        {
                            Session["Result"] = "Error,Try Again";

                        }

                    }
                    return RedirectToAction("ChangeInfor");
                }
                else
                    return RedirectToAction("Home", "Login");
        
            }
            else
                return RedirectToAction("Home","Login");
        }


        }


    }

